class ThermoBeamEffect extends PainterBeamEffect;

var float BurnTime;
var xEmitter Flame;
var xEmitter Flare;

function SetTargetState(EPainterTargetingState NewState)
{
    TargetState = NewState;

    if (TargetState == PTS_Aquired)
    {
	  TargetState = PTS_Marked;
//        GotoState('Aquired');
    }
    else if (TargetState == PTS_Cancelled)
    {
        GotoState('Cancelled');
    }
}

simulated function Tick(float dt)
{
    local Vector BeamDir;
    local Vector X,Y,Z;
    local xWeaponAttachment Attachment;

    if (Role == ROLE_Authority && (Instigator == None || Instigator.Controller == None))
    {
        Destroy();
        return;
    }

    if (Level.NetMode == NM_DedicatedServer)
    {
        StartEffect = Instigator.Location + Instigator.EyeHeight*Vect(0,0,1);
        return;
    }

    // set beam start location
    if ( Instigator == None )
    {
        SetLocation(StartEffect);
    }
    else
    {
        if ( Instigator.IsFirstPerson() && Instigator.Weapon != None )
        {
			if ( Instigator.Weapon.WeaponCentered() )
				SetLocation( Instigator.Location );
			else
			{
				Instigator.Weapon.GetViewAxes(X, Y, Z);
				SetLocation( (Instigator.Location + Instigator.CalcDrawOffset(Instigator.Weapon) + EffectOffset.X * X + Instigator.Weapon.Hand * EffectOffset.Y * Y + EffectOffset.Z * Z) );
			}
            EndEffect = TFThermoProj(Instigator.Weapon).EndEffect;

        }
        else
        {
            Attachment = xPawn(Instigator).WeaponAttachment;
            if (Attachment != None && (Level.TimeSeconds - Attachment.LastRenderTime) < 1)
                SetLocation( Attachment.GetTipLocation() );
            else
                SetLocation( Instigator.Location + Instigator.EyeHeight*Vect(0,0,1) + Normal(EndEffect - Instigator.Location) * 25.0 );
        }
        if (Role == ROLE_Authority) // what clients will use if their instigator is not relevant yet
            StartEffect = Location;
    }

    BeamDir = Normal(EndEffect - Location);
    SetRotation(Rotator(BeamDir));

    mSpawnVecA = EndEffect;


    if (Spot != None)
    {
        Spot.SetLocation(EndEffect - BeamDir*10.0);
    }

    if (TargetState == PTS_Marked)
    {
        if (Brightness == 40.0)
            PlaySound(MarkSound);
        SetBrightness( FMax(FMin(Brightness+dt*100.0, 200.0), 100.0) );
    }
    else
        SetBrightness( 40.0 );

    if (TargetState == PTS_Aquired)
	TargetState = PTS_Marked;
//        GotoState('Aquired');
    else if (TargetState == PTS_Cancelled)
        GotoState('Cancelled');


if(Level.NetMode != NM_DedicatedServer)
	{
		if (Flare == None)
		    Flare = Spawn(class'ThermoFlare');
		else
		{
		    Flare.SetLocation(mSpawnVecA);
		    Flare.LifeSpan = 0.5;
		}

		BurnTime += dt;

		if(BurnTime > 0.05)
		{
			Spawn(class'LinkScorch',,, mSpawnVecA, Rotator(mSpawnVecA - StartEffect));
			BurnTime = 0;
		}

		if(Flame == None)
//			Flame = Spawn(class'ThermoBurn',,, mSpawnVecA);
			Flame = Spawn(class'BotSparks',,, mSpawnVecA, Rotator(mSpawnVecA - StartEffect));
		else
		{
			Flame.SetLocation(mSpawnVecA);
			Flame.LifeSpan = 0.75;
		}
	}

}

state Aquired
{
    simulated function BeginState()
    {
        PlaySound(AquiredSound);
        AmbientSound = None;
        SetTimer(0.4, false);
    }

    simulated function Timer()
    {
	TargetState = PTS_Marked;
//        GotoState('Cancelled');
    }

    simulated function Tick(float dt)
    {
        if (Level.NetMode != NM_DedicatedServer)
        {
            SetBrightness( 200.0 );
            mSizeRange[0] = FMin(mSizeRange[0]+dt*40.0, 16);		//40, 16
        }
    }

    simulated function SetTargetState(EPainterTargetingState NewState)
    {
    }
}

state Cancelled
{
    simulated function BeginState()
    {
        SetTimer(0.4, false);
    }

    simulated function Timer()
    {
        Destroy();
    }

    simulated function Tick(float dt)
    {
        if (Level.NetMode != NM_DedicatedServer)
        {
            SetBrightness( FMax(Brightness-dt*100.0, 40.0) );
            mSizeRange[0] = FMax(mSizeRange[0]-dt*40.0, 1.0);
        }
    }

    simulated function SetTargetState(EPainterTargetingState NewState)
    {
    }
}




simulated function SetBrightness(float b)
{
    Brightness = b;
    mColorRange[0].R = b;
    mColorRange[0].G = b*0.5;
    mColorRange[0].B = b*0.25;
    mColorRange[1] = mColorRange[0];
}

defaultproperties
{
     EffectOffset=(X=20.000000,Y=6.000000)
     MarkSound=None
     mLifeRange(0)=120.000000
     mLifeRange(1)=120.000000
     mColorRange(0)=(B=0,G=0,R=225,A=180)
     mColorRange(1)=(B=0,G=150,R=255)
     SoundVolume=255
}
